package com.epam.mentoring.music.behaviour;

import org.apache.commons.io.FileUtils;
import org.jbehave.core.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.*;
import java.io.File;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MusicUiSteps {

    private static WebDriver driver;

    @AfterScenario(uponOutcome=AfterScenario.Outcome.FAILURE)
    public void afterFailedScenario() {
        driver.quit();
    }

    @Given("$hostport is opened")
    public void openPage(String hostPort) throws Exception{
        String operationSystem = System.getProperty("os.name");
        System.out.println("OS.Name " + operationSystem);
        if (operationSystem.startsWith("Windows")){
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        }   else if(operationSystem.startsWith("Linux")){
            System.setProperty("webdriver.chrome.driver", "chromedriver");
        }
        driver = new ChromeDriver();
        driver.get(hostPort);
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("src/main/resources/com/epam/mentoring/music/ui/scr_main_page.png"));
        System.out.println("Running JBehave");
    }

    @When("$className is visible")
    public void checkClassNameIsVisible(String className){
        WebElement classObject = driver.findElement(By.className(className));
        assertTrue("Object visible with class " + className, classObject.isDisplayed());
    }

    @Then("there is \"$tagName\" tag with $contentText text is there")
    @Aliases(values={"there is \"$tagName\" with $contentText text is there"})
    public void checkTagText(String tagName, String contextText){
        WebElement mainContent = driver.findElement(By.tagName(tagName));
        assertEquals("Checking tag" + tagName, contextText, mainContent.getText());
    }

    @Then("user successfully leaves the page")
    @Aliases(values={"user quits", "user closes page"})
    public void exit(){
        driver.quit();
    }

    @When ("Link $pageLink is clicked")
    public void linkIsClicked(String pageLink){
        driver.findElement(By.linkText(pageLink)).click();
    }

    @Then ("Page $headerName is opened")
    public void checkHeaderTagText(String headerName){
        WebElement artistsHeader = driver. findElement(By.xpath("/html/body/div[2]/div/b"));
        assertEquals("Checking" + headerName + "page header", headerName, artistsHeader.getText());
    }

    @Then ("Page $pageLink has URL \"$pageUrl\"")
    public void TestSwitchLinks(String pageLink, String pageUrl){
        assertEquals("Checking" + pageLink + "page URL", pageUrl, driver.getCurrentUrl());
    }

    @Then ("Count of Artists on Artists page is more than $itemsCount")
    public void TestArtistsCount(int itemsCount){
        assertTrue(itemsCount<driver.findElements(By.xpath("/html/body/div[2]/div/table/tbody/tr/td")).size()/3);
    }

    @When ("there is \"$button\" tag with $tagAttr attribute is there")
    public void searchButtonPresent (String button, String tagAttr) {
        assertTrue("Checking button present", driver.findElement(By.tagName(button))!=null);
        assertEquals("Checking button" + tagAttr + "present", tagAttr, driver.findElement(By.cssSelector("button.btn.btn-success")).getText());
    }

    @Then ("enter \"$query\" into search and click Search button")
    public void searchText (String query) {
        driver.findElement(By.id("query")).clear();
        driver.findElement(By.id("query")).sendKeys(query);
        driver.findElement(By.cssSelector("button.btn.btn-success")).click();
    }

    @Then ("text $text displayed")
    public void verifyTextPresent (String text) {
        assertEquals(text, driver.findElement(By.xpath("//table/tbody/tr[2]/td[1]/ul/li")).getText());
    }

}